package com.doit.day02

import scala.collection.immutable
import scala.io.Source

/**
 * 需求：
 * 1.求每部电影的平均分，按照降序输出
 * 2.求每个用户评过电影的总数，按照降序排列
 * 3.每个用户评分的前三部电影(用户，电影信息，评分值)
 * 4.哪些年份的电影评分(平均分)最高，取最高的前五个年份
 */
object MovieDemo3 {
  def main(args: Array[String]): Unit = {
    /*
   4.哪些年份的电影评分(平均分)最高，取最高的前五个年份
   1.哪些年份的电影分别是多少分
   A  1995   5
   A  1995   4
   A  1995   5
   A  1995   3
   A  1995   2

   B  1995   6
   。。。
   要针对年份去分组  ==》 (1995,list(2,3,4,5,6,4,3,2,4),(1996,list(2,3,4,5,6,4,3,2,4)

     */

    //读文件
    val movie: List[String] = Source.fromFile("C:\\Users\\yi\\Desktop\\scala\\movies.txt").getLines().toList
    val rating: List[String] = Source.fromFile("C:\\Users\\yi\\Desktop\\scala\\ratings.txt").getLines().toList

    //转换将list集合中的movie对象转换成元组(map)  key movie_id，value 就是movie对象  ==》 要把年份单独取出来
    val movie_info: List[Movie1] = movie.map(line => {
        ////1_Toy Story (1995)_Animation|Children's|Comedy
        var movie_id: String = ""
        var movie_name: String =""
        var movie_type: String =""
        var year: String =""
        try{
          val arr: Array[String] = line.split("_")
          movie_id = arr(0)
          year = arr(1).substring(arr(1).length - 5, arr(1).length - 1)
          movie_name = arr(1).substring(0, arr(1).length - 6)
          movie_type = arr(2)
        }catch{
          case e:Exception => println(e)
        }
      Movie1(movie_id, movie_name,year, movie_type)
      })

    //List(Movie1,Movie1,Movie1,Movie1)

    //===> Map(movie_id,year)

    val movie_year: Map[String, String] = movie_info.map(movie => {
      val movie_id: String = movie.movie_id
      val year: String = movie.year
      (movie_id, year)
    }).toMap

    //rating
    val rating_info: List[Rating] = rating.map(line => {
      var user_id: String = ""
      var movie_id: String = ""
      var rating: Double = 0
      var timeStamp: String = ""
      val arr: Array[String] = line.split("_")
      try {
        ////1_1193_5_978300760
        user_id = arr(0)
        movie_id = arr(1)
        rating = arr(2).toDouble
        timeStamp = arr(3)
      } catch {
        case e: Exception => println(e)
      }
      Rating(user_id, movie_id, rating, timeStamp)
    })

    //拿着rating里面的movie_id 去和movie中的movie_id匹配，匹配完成之后，获取到电影的年份
    val temp: List[(String, Double)] = rating_info.map(rating => {
      var year: String = ""
      val movie_id: String = rating.movie_id
      //拿着movie_id==》 获取year
      val flag: Boolean = movie_year.contains(movie_id)
      if (flag) {
        year = movie_year.get(movie_id).get
      }
      //year,rating
      (year, rating.rating)
    })

//    temp.foreach(println)

    //对年份进行分组  (1995,List((1995,7),(1995,3)))
    val year_rating: Map[String, List[(String, Double)]] = temp.groupBy(_._1)


    //4.哪些年份的电影评分(平均分)最高，取最高的前五个年份
    val map: Map[String, Double] = year_rating.map(tp => {
      val year: String = tp._1
      val list: List[(String, Double)] = tp._2
      val listRating: List[Double] = list.map(_._2)
      val avg: Double = (listRating.sum / listRating.size).formatted("%.2f").toDouble
      (year, avg)
    })

    map.toList.sortWith((tp1,tp2)=>tp1._2>tp2._2).take(5).foreach(println)

  }
}

case class Movie1(movie_id: String, movie_name: String,year:String, movie_type: String)